package com.example.park_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.park_management.common.PageParam;
import com.example.park_management.common.Result;
import com.example.park_management.domain.Complaints;
import com.example.park_management.domain.Floors;
import com.example.park_management.service.FloorsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/floors")
public class FloorsController {

    @Resource
    FloorsService floorsService;

    @PostMapping("/")
    public Result add(@RequestBody Floors floors) {
        try{
            if (floorsService.save(floors)){
                return Result.success();
            }
            return Result.fail();
        }catch (Exception e){
            return Result.fail("输入格式异常！");
        }
    }

    @PutMapping("/")
    public Result update(@RequestBody Floors floors) {
        try{
            if (floorsService.updateById(floors)){
                return Result.success();
            }
            return Result.fail();
        }catch (Exception e) {
            return Result.fail("输入格式异常！");
        }
    }

    @DeleteMapping("/{floorId}")
    public Result delete(@PathVariable Integer floorId) {
        if (floorsService.removeById(floorId)){
            return Result.success();
        }
        return Result.fail();
    }

    @PostMapping("/page")
    public Result page(@RequestBody PageParam<Floors> pageParam) {
        Page<Floors> floorsPage = new Page<>(pageParam.getPageNum(),pageParam.getPageSize());


        QueryWrapper<Floors> floorsQueryWrapper = new QueryWrapper<>();
        if (pageParam.getData() != null){
            if (pageParam.getData().getFloorNumber() != null){
                floorsQueryWrapper.eq("floor_number", pageParam.getData().getFloorNumber());
            }
        }
        floorsService.page(floorsPage,floorsQueryWrapper);
        return Result.success(floorsPage.getRecords(),floorsPage.getTotal());
    }

}
